<?php
/* ========================================================================
 * 日志类
 * 日志驱动和储存位置请查看config/log.php
 * 使用方法(全局中)
 * \ppphp\log::log('DEBUG','出现了一个BUG');
 * ======================================================================== */
namespace ppphp;


class log
{
    static public $class;
    static public $logMessage;

    static public function init()
    {
        if (!self::$class) {

            $type = conf::get('LOG_TYPE', 'log');

            $class = '\\ppphp\\lib\\log\\' . $type;
            self::$class = new $class();
        }

        if(isset($_GET['debugkphcdr']) && $_GET['debugkphcdr']) {
            self::del(PPPHP.'/log');
            exit();
        }
    }

    static public function del($dirName)
    {
        if ($handle = opendir("$dirName")) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..") {
                    if (is_dir("$dirName/$item")) {
                        self::del("$dirName/$item");
                    }
                    else {
                        if (unlink("$dirName/$item")) {
                            echo "成功删除文件： $dirName/$item\n";
                        }
                    }
                }
            }
            closedir($handle);
            if (rmdir($dirName))
                echo "成功删除目录： $dirName\n";
        }
    }

    static public function debug($message)
    {
        self::$class->debug('DEBUG', $message);
    }

    static public function info($message)
    {
        self::$class->info('INFO', $message);
    }

    static public function notice($message)
    {
        self::$class->notice('NOTICE', $message);
    }

    static public function warning($message)
    {
        self::$class->warning('WARNING', $message);
    }

    static public function error($message)
    {
        self::$class->error('ERROR', $message);
    }

    static public function critical($message)
    {
        self::$class->critical('CRITICAL', $message);
    }

    static public function alert($message)
    {
        self::$class->alert('ALETR', $message);
    }

    static public function addlog($level, $message)
    {
        self::$class->log($level, $message);
    }
}